/////////////////////////////////////////////////////////////////////////
// File:        PassServers.h
// Purpose:     PassServers class -> Stores PassServer instances and
//                                   gets licenses from html code
//              PassServer class  -> Stores licenses
// Author:      Tukero
// Modified by: 
// Copyright:   (c) Tukero <tukero@gmail.com>
// Licence:     GNU/LGPL v3
/////////////////////////////////////////////////////////////////////////

/* 
Linked Lists to manage Licenses

The two linked lists have this format:

         PassServers
  .-----------------------.   --.
  |      PassServer1      |     |
  |    .-------------.  --+-----+-------------------------------.
  |    |  License1   |    |     |                               |->Node of PassServer
  |    |-------------|  --+-----+-------------------------------'
  |    |  License2   |    |     |--> Node of PassServers 
  |    |-------------|    |     |
  |    |  LicenseN   |    |     |
  |    '-------------'    |     |
  |-----------------------|   --'
  |      PassServer2      |
  |    .-------------.    |
  |    |  License1   |    |
  |    |-------------|    |
  |    |  License2   |    |
  |    |-------------|    |
  |    |  LicenseN   |    |
  |    '-------------'    |
  |-----------------------|
  |      PassServerN      |
  |    .-------------.    |
  |    |  LicenseN   |    |
  |    |     ...     |    |
  |    '-------------'    |
  '-----------------------'
*/


#ifndef SERVERS_H_INCLUDED
#define SERVERS_H_INCLUDED

#ifndef WX_PRECOMP
#  include <wx/wx.h>
#else
#  include <wx/wxprec.h>
#endif

#include "Utils.h"
#include <wx/regex.h>
#include <wx/config.h>
#include <wx/tokenzr.h>
#include <wx/listimpl.cpp>
//#include <wx/strconv.h>
#include <wx/arrstr.h>

#include "License.h"

class PassServer
{
private:
    wxString        m_Url;
public:
    wxArrayString   m_UsernameTags;
    wxArrayString   m_PasswordTags;
    wxArrayString   m_ParserParts;

    PassServer();
    ~PassServer();

    //List of licenses
    LinkListLicense licenseList;

    void clearLicenseList();
    void clearTagList();
    
    wxString GetUrl();

    void    AddUserTags(wxString &txt2Extract);
    void    AddPassTags(wxString &txt2Extract);
    void    AddParserParts(wxString &txt2Extract);
    
    //Splits the URL in a "host" section and a "path" section
    void setFromUrl(wxString URL);
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

WX_DECLARE_LIST(PassServer, LinkListServer);

class PassServers
{
private:
    wxString m_prevUsername;
    wxString m_prevPassword;

    wxString m_currentUsername;
    wxString m_currentPassword;

    int GetPosInitData(const int posInit, const wxString& lineOfData);
    int GetPosFinal(const int posInit, const wxString& lineOfData, const int posAnotherHeader = -1);
    static void LoadServers2List(LinkListServer *serverList, int serverType);
    void ReplaceHtml(wxString &html);
    int appendCurrentLicense(PassServer *server, unsigned int checkN);
public:
    PassServers();
    ~PassServers();

    // List of servers
    LinkListServer m_serverList;    

    // Load all servers from registry
    int Load();

    static void ReplaceTags(PassServer *server, wxString& data, const wxString& userConst, const wxString& passConst);
    // Remove all server from the list
    void clearServerList();

    // Check if the inputData contains a valid license
    bool processInfo(PassServer *server, wxString &inputData, unsigned int checkN);

    

    static void LoadPersonalServers(LinkListServer *serverList);
    static void LoadOficialServers(LinkListServer *serverList);
    
    static void SaveServersFromList(LinkListServer* serverList);
};

#endif // SERVERS_H_INCLUDED
